package cn.xwt.weidisk.service.impl;

import cn.xwt.weidisk.mapper.RecycleMapper;
import cn.xwt.weidisk.pojo.dao.RecycleDo;
import cn.xwt.weidisk.pojo.dao.RecycleFileDo;
import cn.xwt.weidisk.service.RecycleService;
import cn.xwt.weidisk.util.CommonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author 薛慰涛
 */
@Service("recycleService")
public class RecycleServiceImpl implements RecycleService {

    @Resource
    RecycleMapper recycleMapper;

    final int insertSuccess = 1;

    @Override
    public Boolean insertRecycle(RecycleDo recycle) {
        return recycleMapper.insertRecycle(recycle) == insertSuccess;
    }

    @Override
    public List<RecycleFileDo> selectRecycleList(Long uid) {
        List<RecycleFileDo> recycleFileDoList = recycleMapper.selectRecycleList(uid);
        // 移除过期的文件
        recycleFileDoList.removeIf(recycleFile -> CommonUtils.isExpire(recycleFile.getDeleteTime()));
        Collections.reverse(recycleFileDoList);
        return recycleFileDoList;
    }

    @Override
    public void deleteRecycle(Long id, Long uid) {
        recycleMapper.deleteRecycle(id, uid);
    }

    @Override
    public void deleteRecycleList(List<Long> idList, Long uid) {
        for (Long rid : idList) {
            deleteRecycle(rid, uid);
        }
    }

    @Override
    public List<RecycleDo> selectExpireList() {
        List<RecycleDo> recycleDoList = recycleMapper.selectAllRecycleList();
        // 移除未过期的文件
        recycleDoList.removeIf(recycleDo -> !CommonUtils.isExpire(recycleDo.getCreateTime()));
        return recycleDoList;
    }
}
